#!/usr/bin/env bash
set -euo pipefail

# pre-commit: lightweight per-product checks (type-check + lint on the web tree).
# Heavy lifting (agent-review critic, validators) happens in post-commit + pre-push.
# Skip with PRE_COMMIT_SKIP=1.
#
# This hook is intentionally NOT product-specific in its policy. Each
# scaffolded product can layer additional checks here as needed; the default
# is the minimum every product needs (frontend type-check + lint).

if [[ "${PRE_COMMIT_SKIP:-}" == "1" ]]; then
  echo "pre-commit: skipped by PRE_COMMIT_SKIP=1"
  exit 0
fi

# Only run web checks if web/ has staged changes — most commits touch backend
# or infra and shouldn't pay the type-check cost.
WEB_STAGED="$(git diff --name-only --cached --diff-filter=ACMR | grep -E '^web/' || true)"

if [[ -n "${WEB_STAGED}" ]]; then
  if [[ ! -d web ]]; then
    echo "pre-commit: web/ staged changes detected but web/ directory is missing — skipping" >&2
    exit 0
  fi
  echo "pre-commit: web/ changes detected — running type-check + lint"
  ( cd web && npm run type-check )
  ( cd web && npm run lint )
  echo "pre-commit: web/ checks passed"
else
  echo "pre-commit: no web/ changes — skipping type-check + lint"
fi
